﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using BaHuan.BusinessLayer;
using BaHuan.Entities;
using BaHuan.Common;

namespace BaHuan
{
    public partial class FrmTimKiemPhieuChuyenKho : Form
    {
        private PhieuChuyenKhoManagement phieuchuyenkhoMgt = new PhieuChuyenKhoManagement();

        private NhanVien nhanvien;
        public NhanVien NhanVien
        {
            set { this.nhanvien = value; }
            get { return this.nhanvien; }
        }

        public FrmTimKiemPhieuChuyenKho()
        {
            InitializeComponent();
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            DateTime dtTuNgay = dtpTuNgay.Value;
            DateTime dtDenNgay = dtpDenNgay.Value;

            List<PhieuChuyenKho> lstPhieuChuyenKho = (from p in GetAllPhieuChuyenKho()
                                                      where (p.MaSo.ToLower().Contains(txtTimKiem.Text.ToLower()) ||
                                                          p.KhoXuat.ToLower().Contains(txtTimKiem.Text.ToLower()) ||
                                                          p.KhoNhan.ToLower().Contains(txtTimKiem.Text.ToLower())) &&
                                                          p.NgayThang.Date >= dtTuNgay.Date && p.NgayThang.Date <= dtDenNgay.Date
                                                      select p).ToList();

            dgvDanhSachPCK.AutoGenerateColumns = false;
            dgvDanhSachPCK.DataSource = lstPhieuChuyenKho;
        }

        private void btnMoi_Click(object sender, EventArgs e)
        {
            FrmPhieuChuyenKho frmPhieuChuyenKho = new FrmPhieuChuyenKho();
            frmPhieuChuyenKho.NhanVien = this.nhanvien;
            this.Hide();
            frmPhieuChuyenKho.ShowDialog();
            this.Show();
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dgvDanhSachPCK_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            PhieuChuyenKho phieuchuyenkho = (from p in GetAllPhieuChuyenKho()
                                             where p.Id == ((PhieuChuyenKho)dgvDanhSachPCK.Rows[e.RowIndex].DataBoundItem).Id
                                             select p).FirstOrDefault();

            FrmPhieuChuyenKho frmPhieuChuyenKho = new FrmPhieuChuyenKho();
            frmPhieuChuyenKho.NhanVien = this.nhanvien;
            frmPhieuChuyenKho.mphieuchuyenkho = phieuchuyenkho;
            frmPhieuChuyenKho.isEdited = true;
            this.Hide();
            frmPhieuChuyenKho.ShowDialog();
            this.Show();
            dgvDanhSachPCK.DataSource = null;
        }

        public List<PhieuChuyenKho> GetAllPhieuChuyenKho()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = phieuchuyenkhoMgt.GetAllPhieuChuyenKho();
            List<PhieuChuyenKho> listPhieuChuyenKho = (from pck in(List<PhieuChuyenKho>)resultObj.Result
                                                           where pck.IsDelete==0
                                                           select pck).ToList();

            return listPhieuChuyenKho;
        }
    }
}
